<?php

    require_once('simple_html_dom.php');

    define('DB_HOST', 'localhost');
    define('DB_USER', 'root'     );
    define('DB_PASSWD', 'coolchuan2012');
    $the_month = date('Ym');
    define('DB_NAME'       , 'catchappdataall');
    define('DB_TABLE_NAME' , 'jk_apps_search_data_mobile_'.$the_month);
    define('DB_TABLE_GF_INFO', 'jk_apps_info_jifeng');
    //360
    define( 'TSZ_URL_MAIN'  , 'http://zhushou.360.cn/search/index/?kw='             );  
    define( 'TSZ_URL_HEAD'  , 'http://openbox.mobilem.360.cn/AppStore/newSearch?'   );  

    //gfan
    define( 'GFAN_URL_MAIN' , 'http://apk.gfan.com/Aspx/UserApp/'               );  
    define( 'GFAN_URL_TAIL' , '-1.shtml'                                        );  

    // 豌豆荚
    define( 'WDJ_URL_MAIN'  , 'http://www.wandoujia.com/search?key='        );  

    // 百度
    define('BD_URL_MAIN'    , 'http://m.baidu.com/s?st=10a081&tn=webmkt&rn=10&word='        );  
    
    // 安智
    define( 'AZ_URL_MAIN'   , 'http://m.anzhi.com/search.php?keyword=' );

    define( 'TSZ_ID'        , '1' );
    define( 'WANDOUJIA_ID'  , '2' );
    define( 'GFAN_ID'       , '3' );
    define( 'BAIDU_ID'      , '4' );
    define( 'ANZHI_ID'      , '5' );
    define( 'MAX_RECORD'    , '40' );

    
    $random       = $_GET['random'];
    $ip           = $_SERVER['REMOTE_ADDR'];
    $package_name = $_GET['package'];
    $keyword_name = $_GET['keyword'];
    $keyword_id   = $_GET['id'];
    
    if($ip != '121.199.44.73')
        return ;

   // $package_name = 'com.tencent.mm';
   // $keyword_name = '微信';
   // $keyword_id   = '2';

    catchData($keyword_name, $keyword_id, $package_name);

    // 入口函数
    function catchData($keyword_name, $keyword_id, $package_name){

        $keywords = array();
        $today_time = date('Y-m-d');
        $keyword = array('keywords'=>$keyword_name, 'packagename'=>$package_name, 'search_id'=>$keyword_id, 'time'=>$today_time);

        $status_tmp = isChecked($keyword);
        if($status_tmp[1] == false && $status_tmp[2] == false && $status_tmp[3] == false && $status_tmp[4] == false && $status_tmp[5] == false){
            echo "\n这个关键词已经查询过了\n";
            return ;
        }

        $ranks = array( 1=>tsz_getRank($keyword), 2=>wdj_getRank($keyword), 3=>gf_getRank($keyword), 4=>bd_getRank($keyword), 5=>az_getRank($keyword));

        $keyword_status = isChecked($keyword);

        foreach($keyword_status as $index => $status){
            if($status){
                $rank = $ranks[$index];
                $keywords[$index] = array_merge($keyword, array('rank'=>$rank));
            }                
        }

        insertData($keywords, $keyword_status, $keyword_id);
        
    }

    function gf_getRank($keyword){
        $db_obj = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
        $sql = 'select app_id from ' . DB_TABLE_GF_INFO . ' where packagename="' . $keyword['packagename'] . '"';
        if($mk_list = $db_obj->query($sql)){
          if($row = $mk_list->fetch_assoc())
            $keyword  = array_merge($keyword, array( 'app_id' => $row['app_id'] ));
          else
            $keyword  = array_merge($keyword, array( 'app_id' => -1 ));
        }

        // 计算最大查询的页数
		$page_num = intval(MAX_RECORD / 30) + 1 ;

		// 生成url
			$url = GFAN_URL_MAIN . $keyword['keywords'];
            $rank = 0;

			    for($index=1; $index<=$page_num; $index++){

				    $url_tmp  = $url . '-' . $index . GFAN_URL_TAIL; 
                    $html = file_get_html($url_tmp);
                    if(false == $html){
                        continue;
                    }
				    $app_list = $html->find('ul.appList li a');
                    $rank_tmp = 0; 

                    // 测试是否这一页还有数据，如果没有直接搜索下一个关键词。
                    if( count($app_list) < 1 )
                        break;
                    // 如果这页有数据，开始遍历数据。
                    foreach($app_list as $list_num => $app){
                        $rank_tmp = ($index-1) * 30 + $list_num + 1;
                        if($rank_tmp > 40)
                            break;
                        if (gf_isTarget($app->getAttribute('href'), $keyword)){
                            $index = $page_num + 1;
                            $rank = $rank_tmp; 
                            break;
                        }
				    }
			    }

		$html->clear();
		unset($html);
        return $rank;
    }

    function bd_getRank($keyword){
		// 计算最大查询的页数
		$page_num = intval(MAX_RECORD / 10) ;

		// 生成url
            $keyword_tmp = urlencode($keyword['keywords']);
			$url = BD_URL_MAIN . $keyword_tmp;
	        $rank = 0;

			    for($index=0; $index<$page_num; $index++){
                    $pn_tmp = $index * 10;
				    $url_tmp  = $url . '&pn=' . $pn_tmp;
                    $html = file_get_html($url_tmp); 
                    if(false == $html){
                        continue;
                    }
				    $app_list = $html->find('ul.dataList li a.list-a');
				    $i = 0;
                    foreach($app_list as $list_num => $app){
                        if (bd_isTarget($app->getAttribute('href'), $keyword)){
                            $rank = $index * 10 + $list_num + 1;
                            $index = $page_num + 1;
                            break;
                        }
				    }
			    }
            

		$html->clear();
		unset($html);
        return $rank;
    }

    function az_getRank($keyword){
		// 计算最大查询的页数
		$page_num = intval(MAX_RECORD / 15) ;

		// 生成url
            $rank = 0;
            $keyword_tmp = urlencode($keyword['keywords']);
			$url = AZ_URL_MAIN . $keyword_tmp;

            for($index=0; $index<=$page_num; $index++){

                $url_tmp  = $url . '&morelist=' . $index;
                $html = file_get_html($url_tmp);
                if(false == $html){
                    continue;
                }
                $app_list = $html->find('li div.itmes-icon img');
                $rank_tmp = 0; 

                // 测试是否这一页还有数据，如果没有直接搜索下一个关键词。
                if( count($app_list) < 1 )
                    break;

                // 如果这页有数据，开始遍历数据。
                foreach($app_list as $list_num => $app){
                    $rank_tmp = $index * 15 + $list_num + 1;
                    if($rank_tmp > 40)
                        break;
                    if (isTarget($app->getAttribute('src'), $keyword)){
                        $index = $page_num + 1;
                        $rank = $rank_tmp; 
                        break;
                    }
                }
            }

		$html->clear();
		unset($html);
        return $rank;
    }

    function wdj_getRank($keyword){
		// 计算最大查询的页数
		$page_num = intval(MAX_RECORD / 9 )+ 1;


		// 生成url
			$url = WDJ_URL_MAIN . $keyword['keywords'];
            $rank = 0;
		
            for($index=1; $index<=$page_num; $index++){
                $url_temp  = $url . '&page=' . $index;
                $html = file_get_html($url_temp); 
                if(false == $html){
                    continue;
                }
                $app_list = $html->find('.large-list .app');
                $i = 0;
                foreach($app_list as $app){
                    // testFunc($app);
                    $i++;   
                    if($keyword['packagename'] == $app->getAttribute('data-pn')){
                        $rank = ($index-1) * 9 + $i;
                        $index = $page_num + 1;
                        break;
                      }
                }
            }



		$html->clear();
		unset($html);

        return $rank;
    }

    function tsz_getRank($keyword){

		    // 生成url
			$rank = 0;
            $words_tmp = iconv('utf-8', 'gb2312', $keyword['keywords']);
			// 编码
		    $words_tmp = urlencode($words_tmp);
			$url_list = TSZ_URL_HEAD . 'kw=' . $words_tmp . '&start=0&count=' . MAX_RECORD;
			$html = file_get_contents($url_list);
			$app_list = json_decode($html)->data;	
			// testFunc($app_list);
			
			foreach($app_list as $i => $app) {
				if( $app->apkid == $keyword['packagename'] ){
					$rank = $i + 1;
					break;	
				}
			}
            return $rank;
    }

    // 输入插入
    function insertData($keywords, $keyword_status, $search_id){
        $db_obj = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
        foreach($keyword_status as $index => $status){
            if($status == true){
               $sql = 'insert into ' . DB_TABLE_NAME . '(search_id, market_id, rank, time) values(' . $keywords[$index]['search_id'] . ',' . $index . ',' . $keywords[$index]['rank'] . ',"' . $keywords[$index]['time'] . '")';
               if(!$db_obj->query($sql)){
                   echo "\n 在insertData函数中, $i:".$index."条sql语句中插入数据库失败! \n";
                   continue;
               }
            }
        }
    
        // 同步
        syncData($search_id);
    }

    // 同步
    function syncData($search_id){
        if(!is_dir('./sql'))
            mkdir('./sql');
        $cmd = 'mysqldump -t --tz-utc=0 -uroot -pcoolchuan2012 -w"search_id=2 and time=\"$(date +\'%Y-%m-%d\')\"" catchappdataall jk_apps_search_data_mobile_$(date +\'%Y%m\') > ./sql/search_data_$(date +\'%Y%m%d\')_'.$search_id.'.sql ';
        shell_exec($cmd);

        $server = 'work@121.199.44.73';
        $sender = './sql/*';
        $file_name = '/data/www/db/search_data_' . date("Ymd") . '_' . $search_id . '.sql';
        echo "\n" . $file_name . "\n";
        // 同步
        $cmd = 'rsync '.$sender.' '.$server.':/data/www/db/';
        shell_exec($cmd);
        // 执行导入sql命令
        $cmd = 'ssh ' .$server . ' "mysql -uroot -pcoolchuan2012 coolweb_jk < '.$file_name . '"';
        shell_exec($cmd);
        echo "\n脚本执行完毕\n";                
    }
    
    // 数据完整性校验
    function isCompleted($keyword){
      if(empty($keyword['id']) || empty($keyword['packagename']) || empty($keyword['keywords']) || $keyword['rank'] < 0)  
          return false;
      return true;
    }

    // 数据重复性校验
    function isChecked($keyword){
        $db_obj = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);     
        $keyword_status = array();
        for($i=1; $i<=5; $i++){
            $sql = 'select count(*) from ' . DB_TABLE_NAME . ' where search_id=' . $keyword['search_id'] . ' and time="' . $keyword['time'] . '" and market_id=' . $i ; 
            if($result = $db_obj->query($sql)){
                $row = $result->fetch_row();
                if($row[0] == 0)
                    $keyword_status[$i] = true;
                else
                    $keyword_status[$i] = false;
            }else{
                echo "\n在isChecked函数中，索引i:".$i." 条SQL查询数据库失败！\n";
            }
        }

        if(count($keyword_status) == 5)
            return $keyword_status;
        else
            return false;

    }
    
    function isTarget($content_url, $keyword){
       if( false != strpos($content_url, $keyword['packagename'])){
               return true;
       } else {
               return false;
        }
    }

    function gf_isTarget($content_url, $keyword){
       if( false != strpos($content_url, $keyword['app_id'])){
               return true;
       } else {
               return false;
        }
    }
    
    function bd_isTarget($content_url, $keyword){
             $html = file_get_html($content_url);
             $ret_list = $html->find('td.col-content a.event-exe-install'); 
             foreach($ret_list as $name){
                        if( $keyword['packagename'] == $name->getAttribute('data-package') ){
                             return true;
                        }
              }
                                
            $html->clear();
            return false;
    }

?>
